﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace XHFrame
{
    /// <summary>
    /// 联系处理
    /// </summary>
    public class TouchHandle
    {
        private event OnTouchEventHandle eventHandle = null;
        private object[] handleParams = null;

        /// <summary>
        /// 销毁处理
        /// </summary>
        public void DestoryHandle()
        {
            if (null != eventHandle)
            {
                eventHandle -= eventHandle;
                eventHandle = null;
            }
        }

        /// <summary>
        /// 设置消息处理
        /// </summary>
        /// <param name="_handle">返回委托信息</param>
        /// <param name="_params">扩展参数</param>
        public void SetEventHandle(OnTouchEventHandle _handle, params object[] _params)
        {
            DestoryHandle();
            eventHandle += _handle;
            handleParams = _params;
        }
        /// <summary>
        /// 调用事件处理
        /// </summary>
        /// <param name="_listener">事件触发监听</param>
        /// <param name="_args"></param>
        public void CallEventHandle(EventTriggerListener _listener, object _args)
        {
            if (null != eventHandle)
            {
                eventHandle(_listener.gameObject, _args, handleParams);
            }
        }
        /// <summary>
        /// 事件处理构造
        /// </summary>
        /// <param name="_handle">联系处理事件委托</param>
        /// <param name="_params">可扩展参数</param>
        public TouchHandle(OnTouchEventHandle _handle, params object[] _params)
        {
            SetEventHandle(_handle, _params);
        }
        /// <summary>
        /// 事件处理构造
        /// </summary>
        public TouchHandle()
        {

        }
    }


    /// <summary>
    /// 事件触发监听
    /// </summary>
    public class EventTriggerListener : MonoBehaviour,
        IPointerClickHandler,
        IPointerDownHandler,
        IPointerUpHandler,
        IPointerEnterHandler,
        IPointerExitHandler,

        ISelectHandler,
        IUpdateSelectedHandler,
        IDeselectHandler,

        IDragHandler,
        IEndDragHandler,
        IDropHandler,
        IScrollHandler,
        IMoveHandler

    {
        public TouchHandle onClick;
        public TouchHandle onDoubleClick;
        public TouchHandle onDown;
        public TouchHandle onEnter;
        public TouchHandle onExit;
        public TouchHandle onUp;
        public TouchHandle onSelect;
        public TouchHandle onUpdateSelect;
        public TouchHandle onDeSelect;
        public TouchHandle onDrag;
        public TouchHandle onDragEnd;
        public TouchHandle onDrop;
        public TouchHandle onScroll;
        public TouchHandle onMove;
        /// <summary>
        /// 获取或添加EventTriggerListener 事件触发的监听
        /// </summary>
        /// <param name="go">监听的对象</param>
        /// <returns></returns>
        public static EventTriggerListener Get(GameObject go)
        {
            return go.GetOrAddComponent<EventTriggerListener>(); // 获取或添加EventTriggerListener
        }

        /// <summary>
        /// 销毁所有事件监听处理
        /// </summary>
        public void OnDestory()
        {
            this.RemoveAllHandle();
        }

        void RemoveAllHandle()
        {
            this.RemoveHandle(onClick);
            this.RemoveHandle(onDoubleClick);
            this.RemoveHandle(onDown);
            this.RemoveHandle(onEnter);
            this.RemoveHandle(onExit);
            this.RemoveHandle(onUp);
            this.RemoveHandle(onDrop);
            this.RemoveHandle(onDrag);
            this.RemoveHandle(onDragEnd);
            this.RemoveHandle(onScroll);
            this.RemoveHandle(onMove);
            this.RemoveHandle(onUpdateSelect);
            this.RemoveHandle(onSelect);
            this.RemoveHandle(onDeSelect);
        }


        /// <summary>
        /// 移除处理
        /// </summary>
        /// <param name="_handle"></param>
        void RemoveHandle(TouchHandle _handle)
        {
            if (null != _handle)
            {
                _handle.DestoryHandle();
                _handle = null;
            }
        }

        #region 重写GUI事件接口
        void IDeselectHandler.OnDeselect(BaseEventData eventData)
        {
            if (null != onDoubleClick)
            {
                onDoubleClick.CallEventHandle(this, eventData);
            }
        }

        void IDragHandler.OnDrag(PointerEventData eventData)
        {
            if (null != onDrag)
            {
                onDrag.CallEventHandle(this, eventData);
            }
        }

        void IDropHandler.OnDrop(PointerEventData eventData)
        {
            if (null != onDrop)
            {
                onDrop.CallEventHandle(this, eventData);
            }
        }

        void IEndDragHandler.OnEndDrag(PointerEventData eventData)
        {
            if (null != onDragEnd)
            {
                onDragEnd.CallEventHandle(this, eventData);
            }
        }

        void IMoveHandler.OnMove(AxisEventData eventData)
        {
            if (null != onMove)
            {
                onMove.CallEventHandle(this, eventData);
            }
        }

        void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
        {
            if (null != onClick)
            {
                onClick.CallEventHandle(this, eventData);
            }
        }



        void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
        {
            if (null != onDown)
            {
                onDown.CallEventHandle(this, eventData);
            }
        }

        void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
        {
            if (null != onEnter)
            {
                onEnter.CallEventHandle(this, eventData);
            }
        }

        void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
        {
            if (null != onExit)
            {
                onExit.CallEventHandle(this, eventData);
            }
        }

        void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
        {
            if (null != onUp)
            {
                onUp.CallEventHandle(this, eventData);
            }
        }

        void IScrollHandler.OnScroll(PointerEventData eventData)
        {
            if (null != onScroll)
            {
                onScroll.CallEventHandle(this, eventData);
            }
        }

        void ISelectHandler.OnSelect(BaseEventData eventData)
        {
            if (null != onSelect)
            {
                onSelect.CallEventHandle(this, eventData);
            }
        }

        void IUpdateSelectedHandler.OnUpdateSelected(BaseEventData eventData)
        {
            if (null != onUpdateSelect)
            {
                onUpdateSelect.CallEventHandle(this, eventData);
            }
        }

        #endregion
        /// <summary>
        /// 设置事件处理
        /// </summary>
        /// <param name="_type">触发事件类型</param>
        /// <param name="_hadle">返回委托信息</param>
        /// <param name="_params">可扩展参数</param>
        public void SetEventHandle(EnumTouchEventType _type, OnTouchEventHandle _hadle, params object[] _params)
        {
            switch (_type)
            {
                case EnumTouchEventType.OnClick:
                    if (null == onClick)
                        onClick = new TouchHandle();
                    onClick.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnDoubleClick:
                    if (null == onDoubleClick)
                        onDoubleClick = new TouchHandle();
                    onDoubleClick.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnDown:
                    if (null == onDown)
                        onDown = new TouchHandle();
                    onDown.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnUp:
                    if (null == onUp)
                        onUp = new TouchHandle();
                    onUp.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnEnter:
                    if (null == onEnter)
                        onEnter = new TouchHandle();
                    onEnter.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnExit:
                    if (null == onExit)
                        onExit = new TouchHandle();
                    onExit.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnSelect:
                    if (null == onSelect)
                        onSelect = new TouchHandle();
                    onSelect.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnUpdateSelect:
                    if (null == onUpdateSelect)
                        onUpdateSelect = new TouchHandle();
                    onUpdateSelect.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnDeSelect:
                    if (null == onDeSelect)
                        onDeSelect = new TouchHandle();
                    onDeSelect.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnDrag:
                    if (null == onDrag)
                        onDrag = new TouchHandle();
                    onDrag.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnDragEnd:
                    if (null == onDragEnd)
                        onDragEnd = new TouchHandle();
                    onDragEnd.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnDrop:
                    if (null == onDrop)
                        onDrop = new TouchHandle();
                    onDrop.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnScroll:
                    if (null == onScroll)
                        onScroll = new TouchHandle();
                    onScroll.SetEventHandle(_hadle, _params);
                    break;
                case EnumTouchEventType.OnMove:
                    if (null == onMove)
                        onMove = new TouchHandle();
                    onMove.SetEventHandle(_hadle, _params);
                    break;
                default:
                    break;
            }
        }
    }
}
